package tnp;

import tnp.model.SessionObject;
import tnp.modules.ServiceModule;
import tnp.modules.ApplicationModule;
import tnp.modules.DataSourcesModule;
import tnp.util.InitPath;

import org.apache.log4j.xml.DOMConfigurator;

import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.guice.GuiceComponentInjector;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.settings.IMarkupSettings;

import com.google.code.ibaguice.dao.TransactionalModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Stage;


/**
 */
public class TnpApplication extends WebApplication
{
	private Injector injector;

	/**
	 */
	public TnpApplication()
	{

	}

	@Override
	protected void init() 
	{
		super.init();

		String realPaath = System.getProperty(InitPath.DEFAULT_WEB_APP_ROOT_KEY);
		DOMConfigurator.configure( realPaath+"\\WEB-INF\\classes\\"+"log4j.xml" );
		injector = Guice.createInjector(
				this.getConfigurationType().equals(Application.DEVELOPMENT) ? Stage.DEVELOPMENT : Stage.PRODUCTION,
				new ApplicationModule(),
				new ServiceModule(),
				new TransactionalModule(),
				new DataSourcesModule());

		addComponentInstantiationListener(new GuiceComponentInjector(this, injector));

		getRequestCycleSettings().setResponseRequestEncoding("UTF-8");
		IMarkupSettings markupSettings = getMarkupSettings();

		markupSettings.setDefaultMarkupEncoding("UTF-8");
	}

	@Override
	public Session newSession(final Request request, final Response response)
	{
		return new SessionObject(request);
	}
	@Override
	public RequestCycle newRequestCycle(Request request, Response response)
	{
		return new TnpRequestCycle(this, (WebRequest) request, response);
	}

	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class< ? extends Page> getHomePage()
	{
		return HomePage.class;
	}
}
